package com.yzdg.gateway.auth;

import cn.dev33.satoken.stp.StpInterface;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

@Component
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        String authPermissionPrefix = "auth.permission";
        String authKey = authPermissionPrefix + "." + loginId.toString();
        String authValue = redisTemplate.opsForValue().get(authKey);
        // 如果redis中没有值，则返回空列表
        if (StringUtils.isBlank(authValue)){
            return Collections.emptyList();
        }
        return new Gson().fromJson(authValue, List.class);
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        String authRolePrefix = "auth.role";
        String authKey = authRolePrefix + "." + loginId.toString();
        String authValue = redisTemplate.opsForValue().get(authKey);
        // 如果redis中没有值，则返回空列表
        if (StringUtils.isBlank(authValue)){
            return Collections.emptyList();
        }
        return new Gson().fromJson(authValue, List.class);
    }

}
